﻿using StartEAS_API.Utils;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.NoticeD
{
    public class NoticeDAL
    {
        private readonly StarsEASContext _context;
        public NoticeDAL(StarsEASContext context) {
            _context = context;
        }

        public ResultUtils GetAllNotice() {
            StarsEASContext _context = new StarsEASContext();
            AppLog.WriteInfo($"查询通知类别---查询所有通知类别", true);
            return new ResultUtils() { code = 200, message = _context.Notices.ToList() };
        }

        public ResultUtils GetAllNoticeByNoticeId(int NoticeId)
        {
            StarsEASContext _context = new StarsEASContext();
            AppLog.WriteInfo($"查询通知类别---查询{NoticeId}通知类别", true);
            return new ResultUtils() { code = 200, message = _context.Notices.Find(NoticeId) };
        }

        public ResultUtils InsertNotice(Notice notice) {
            StarsEASContext _context = new StarsEASContext();
            if (notice == null) {
                AppLog.WriteInfo($"新增通知类别---未输入通知类别信息", true);
                return new ResultUtils() { code=500,message="请检查通知类别信息"};
            }
            notice.NoticeId = 0;
            _context.Notices.Add(notice);
            int result = _context.SaveChanges();
            if (result == 0) {
                AppLog.WriteError($"新增通知类别---新增失败", true);
                return new ResultUtils() { code = 500, message = "新增失败" };
            }
            AppLog.WriteInfo($"新增通知类别---新增{notice.NoticeName}成功", true);
            return new ResultUtils() { code = 200, message = result };
        }

        public ResultUtils UpdateNotice(Notice data) {
            StarsEASContext _context = new StarsEASContext();
            if (data == null) {
                AppLog.WriteError($"修改通知类别---未输入类别信息", true);
                return new ResultUtils() { code = 500, message = "请输入类别信息" };
            }
            Notice notice = _context.Notices.Find(data.NoticeId);
            if (notice == null) {
                AppLog.WriteError($"修改通知类别---未查询到{data.NoticeId}对应的信息", true);
                return new ResultUtils() { code=500,message="未查询到对应的信息"};
            }
            notice.NoticeName = data.NoticeName;
            int result = _context.SaveChanges();
            if (result == 0)
            {
                AppLog.WriteError($"修改通知类别---修改失败或与原数据一致", true);
                return new ResultUtils() { code = 500, message = "修改失败或与原数据一致" };
            }
            AppLog.WriteInfo($"修改通知类别---修改{notice.NoticeName}成功", true);
            return new ResultUtils() { code = 200, message = result };
        }
    }
}
